// 显示登录界面
function showLogin() {
	$.blockUI({
		message : $('#user-login'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 300) / 2 + 'px',
			cursor : 'auto',
			width : '300px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
}

// 自动登录
function autoLogin() {
	var logininfo = $.cookie('logininfo-cookie');
	if (isEmpty(logininfo)) {
		return;
	}
	var login = logininfo.split("-");
	var name = login[0];
	var pass = login[1];
	ajaxLogin(name, pass);
}

// 用户登录
function login() {
	var name = $('#loginname').val();
	var pass = $('#password').val();
	var is_remenber = $('#is_remenber:checked').val();
	if (isEmpty(name) || isEmpty(pass)) {
		blockAlert("请填写完整的登录信息");
		return;
	}
	closeBlockUI();
	pass = encrypt(name, pass);
	if (!isEmpty(is_remenber)) {
		$.cookie('logininfo-cookie', name + "-" + pass, {
			expires : 100
		});
	} else {
		$.cookie('logininfo-cookie', null);
	}
	// 加密密码
	ajaxLogin(name, pass);
}

// Ajax登录
function ajaxLogin(name, pass) {
	if (isEmpty(name) || isEmpty(pass)) {
		blockAlert("请填写完整的登录信息");
		return;
	}
	$.ajax({
		url : rootPath + "/user/ajaxLogin",
		data : {
			loginname : name,
			password : pass
		},
		dataType : "JSON",
		type : "POST",
		async : false,
		success : ajaxLogin_back
	});
}

function ajaxLogin_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#loginname').val("");
	$('#password').val("");
	$('#userinfo-login-name').html(res.loginname);
	$('#userinfo').show();
	$('#userinfo_login').hide();
	$('#myGuideTitle').click();
	$('#guide-title1').show();
	ajaxLoadGuide();
}

// 用户登出
function logout() {
	$.ajax({
		url : rootPath + "/user/ajaxLogout",
		dataType : "JSON",
		type : "POST",
		success : logout_back
	});
}

function logout_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$.cookie('logininfo-cookie', null);
	$.cookie('logininfo-isShow-cookie', null);
	isLogin = 0;
	$('#my-guide-show').html("请登录");
	$('#userinfo > div').first().html("");
	$('#userinfo').hide();
	$('#userinfo_login').show();
	$('#hotGuideTitle').click();
}

// 用户显示快速注册界面
function fastRegist() {
	$.blockUI({
		message : $('#fast-regist'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 300) / 2 + 'px',
			cursor : 'auto',
			width : '300px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
}

// 用户注册
function ajaxRegist() {
	var name = $('#regist-loginname').val();
	var pass = $('#regist-password').val();
	var pass2 = $('#regist-password2').val();

	if (isEmpty(name) || isEmpty(pass) || pass != pass2) {
		$('#msg').html("信息不正确");
		return;
	}
	$.ajax({
		url : rootPath + "/user/ajaxRegist",
		data : {
			loginname : name,
			password : encrypt(name, pass)
		},
		dataType : "JSON",
		type : "POST",
		success : ajaxRegist_back
	});
}

function ajaxRegist_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	blockAlert("注册成功，请登录");
	closeBlockUI();
}

// 加载分类列表
function ajaxLoadCatgList() {
	$.ajax({
		url : rootPath + "/guide/ajaxLoadCatgList",
		dataType : "JSON",
		type : "POST",
		success : ajaxLoadCatgList_back
	});
}

function ajaxLoadCatgList_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	var htmlStr = "";
	htmlStr += "<option value='-1'>请选择分类</option>";
	$.each(res.gcdList, function() {
		htmlStr += "<option value='" + this.catgUserId + "'>" + this.catgUserTitle + "</option>";
	});
	$('#guide-catg-select').html(htmlStr);
}

// 添加分类
function ajaxAddCatg() {
	var catg = $('#guide-catg-add').val();
	if (isEmpty(catg)) {
		blockAlert('请输入分类标题');
		return;
	}
	var dt = {
		catg : catg
	};
	$.ajax({
		url : rootPath + "/guide/ajaxAddCatg",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxAddCatg_back
	});
}

function ajaxAddCatg_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	ajaxLoadCatgList();
	hideAddCatg();
	blockAlert("添加成功");
}

// 显示修改分类界面
function showModifyCatg(catg_id) {
	$('#block-title-catg').html("修改分类");
	$('#block-input-add-catg').hide();
	$('#block-input-modify-catg').show();
	$('#block-input-hidden-catg-id').val(catg_id);
	if (isEmpty(catg_id)) {
		blockAlert('请输入分类标题');
		return;
	}
	var dt = {
		catg_id : catg_id
	};
	$.ajax({
		url : rootPath + "/guide/ajaxGetCatgById",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : showModifyCatg_back
	});
	$.blockUI({
		message : $('#add-catg'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 300) / 2 + 'px',
			cursor : 'auto',
			width : '300px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
}

// 修改分类
function showModifyCatg_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#guide-title-catg').val(res.gcd.catgUserTitle);
}

function ajaxModifyCatg() {
	var catg_id = $('#block-input-hidden-catg-id').val();
	var title = $('#guide-title-catg').val();
	if (isEmpty(catg_id)) {
		blockAlert('请选择分类标题');
		return;
	}
	if (isEmpty(title)) {
		blockAlert('请填写网址名称');
		$('#guide-title-catg').focus();
		return;
	}
	var dt = {
		catg_id : catg_id,
		title : title
	};
	$.ajax({
		url : rootPath + "/guide/ajaxModifyCatg",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxModifyCatg_back
	});
}

function ajaxModifyCatg_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	closeBlockUI();
	$('#guide-catg-a-' + res.gcDto.catgUserId).html(res.gcDto.catgUserTitle);
}

// 删除分类
function ajaxDeleteCatg(catg_id) {
	if (isEmpty(catg_id)) {
		blockAlert('请选择分类标题');
		return;
	}
	var dt = {
		catg_id : catg_id
	};
	$.ajax({
		url : rootPath + "/guide/ajaxDeleteCatg",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxDeleteCatg_back
	});
}

function ajaxDeleteCatg_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
}

// 加载热门导航项
function ajaxLoadHotGuide() {
	$.ajax({
		url : rootPath + "/guide/ajaxLoadHotGuide",
		dataType : "JSON",
		type : "POST",
		success : ajaxLoadHotGuide_back
	});
}

function ajaxLoadHotGuide_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#hot-guide-show').html("");
	var i = 0;
	$.each(res.gchdList, function(n, gchd) {
		if (this.gihdList.length != 0) {
			if (i == 0) {
				webAddCatgWithOutBorderHot(gchd.catgHotId, gchd.catgHotTitle);
			} else {
				webAddCatgHot(gchd.catgHotId, gchd.catgHotTitle);
			}
			$.each(this.gihdList, function(nn, gih) {
				webAddItemHot(gih.catgHotId, gih.itemHotId, gih.itemHotTitle, gih.itemHotUrl);
			});
			i++;
		}
	});
}

function webAddCatgHot(id, title) {
	var htmlStr = "";
	htmlStr += "<div class='guide-center-catg'>";
	htmlStr += "<div class='guide-left fleft'>";
	htmlStr += "<div class='guide-catg'>";
	htmlStr += "<div>";
	htmlStr += title;
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "<div class='guide-right fleft' id='guide-hot-catg-" + id + "'>";
	htmlStr += "<div class='guide-item guide-item-add fleft'>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#hot-guide-show').append(htmlStr);
}

function webAddCatgWithOutBorderHot(id, title) {
	var htmlStr = "";
	htmlStr += "<div class='guide-center-catg'>";
	htmlStr += "<div class='guide-left fleft' style='border: 0'>";
	htmlStr += "<div class='guide-catg'>";
	htmlStr += "<div>";
	htmlStr += title;
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "<div class='guide-right fleft' style='border: 0' id='guide-hot-catg-" + id + "'>";
	htmlStr += "<div class='guide-item guide-item-add fleft'>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#hot-guide-show').append(htmlStr);
}

function webAddItemHot(catgId, id, title, url) {
	var htmlStr = "";
	htmlStr += "<div class='guide-item fleft'>";
	htmlStr += "<div class='fleft'>";
	htmlStr += "<a href='" + url + "' target='_blank' onclick='ajaxAddVisitCnt(\"" + id + "\")'>" + title + "</a>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#guide-hot-catg-' + catgId + ' > div').last().prev().before(htmlStr);
}

// 加载自定义导航项
function ajaxLoadGuide() {
	$.ajax({
		url : rootPath + "/guide/ajaxLoadGuide",
		dataType : "JSON",
		type : "POST",
		success : ajaxLoadGuide_back
	});
}

function ajaxLoadGuide_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#my-guide-show').html("");
	var i = 0;
	$.each(res.gcdList, function(n, gcd) {
		if (this.gidList.length != 0) {
			if (i == 0) {
				webAddCatgWithOutBorder(gcd.catgUserId, gcd.catgUserTitle);
			} else {
				webAddCatg(gcd.catgUserId, gcd.catgUserTitle);
			}
			$.each(this.gidList, function(nn, gid) {
				webAddItem(gcd.catgUserId, gid.itemUserId, gid.itemUserTitle, gid.itemUserUrl);
			});
			i++;
		}
	});
}

function webAddCatg(id, title) {
	var htmlStr = "";
	htmlStr += "<div class='guide-center-catg'>";
	htmlStr += "<div class='guide-left fleft' id='guide-catg-title-'" + id + ">";
	htmlStr += "<div class='guide-catg'>";
	htmlStr += "<div id='guide-catg-a-" + id + "'>";
	htmlStr += title;
	htmlStr += "</div>";
	htmlStr += "<div class='guide-modify'>";
	htmlStr += "<a href='javascript: showModifyCatg(\"" + id + "\")'>改</a>";
	htmlStr += " / ";
	htmlStr += "<a href='javascript: ajaxDeleteCatg(\"" + id + "\")'>删</a>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "<div class='guide-right fleft' id='guide-catg-" + id + "'>";
	htmlStr += "<div class='guide-item guide-item-add fleft' id='guide-item-add-" + id + "'>";
	htmlStr += "<a href='javascript: showAddItem(\"" + id + "\");'>添加</a>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#my-guide-show').append(htmlStr);
}

function webAddCatgWithOutBorder(id, title) {
	var htmlStr = "";
	htmlStr += "<div class='guide-center-catg'>";
	htmlStr += "<div class='guide-left fleft' style='border: 0' id='guide-catg-title-'" + id + ">";
	htmlStr += "<div class='guide-catg'>";
	htmlStr += "<div id='guide-catg-a-" + id + "'>";
	htmlStr += title;
	htmlStr += "</div>";
	htmlStr += "<div class='guide-modify'>";
	htmlStr += "<a href='javascript: showModifyCatg(\"" + id + "\")'>改</a>";
	htmlStr += " / ";
	htmlStr += "<a href='javascript: ajaxDeleteCatg(\"" + id + "\")'>删</a>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "</div>";
	htmlStr += "<div class='guide-right fleft' style='border: 0' id='guide-catg-" + id + "'>";
	htmlStr += "<div class='guide-item guide-item-add fleft' id='guide-item-add-" + id + "'>";
	htmlStr += "<a href='javascript: showAddItem(\"" + id + "\");'>添加</a>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#my-guide-show').append(htmlStr);
}

function webAddItem(catgId, id, title, url) {
	var htmlStr = "";
	htmlStr += "<div class='guide-item fleft' id='guide-item-" + id + "'>";
	htmlStr += "<div class='fleft' id='guide-item-a-" + id + "'>";
	htmlStr += "<a href='" + url + "' target='_blank' onclick='ajaxAddVisitCnt(\"" + id + "\")'>" + title + "</a>";
	htmlStr += "</div>";
	htmlStr += "<div class='guide-modify fright'>";
	htmlStr += "<a href='javascript: showModifyItem(\"" + id + "\")'>改</a>";
	htmlStr += " / ";
	htmlStr += "<a href='javascript: ajaxDeleteItem(\"" + id + "\")'>删</a>";
	htmlStr += "</div>";
	htmlStr += "<div class='cboth'></div>";
	htmlStr += "</div>";
	$('#guide-catg-' + catgId + ' > div').last().prev().before(htmlStr);
}

// 显示添加网址界面
function showAddItem(id) {
	$('#block-title-item').html("添加网址");
	$('#block-input-add-item').show();
	$('#block-input-modify-item').hide();
	$('#guide-catg-select').val(id);
	$.blockUI({
		message : $('#add-item'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 500) / 2 + 'px',
			cursor : 'auto',
			width : '500px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
}

// 添加网址
function ajaxAddItem() {
	var title = $('#guide-title-item').val();
	var url = $('#guide-url-item').val();
	var catg = $('#guide-catg-select').val();
	if (isEmpty(title)) {
		blockAlert('请填写网址名称');
		$('#guide-title-item').focus();
		return;
	}
	if (isEmpty(url)) {
		blockAlert('请填写网址地址');
		$('#guide-url-item').focus();
		return;
	}
	if (isEmpty(catg) || catg == '-1') {
		blockAlert('请选择网址分类');
		return;
	}
	var dt = {
		title : title,
		url : url,
		catg : catg
	};
	$.ajax({
		url : rootPath + "/guide/ajaxAddItem",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxAddItem_back
	});
}

function ajaxAddItem_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	closeBlockUI();
	ajaxLoadGuide();
	// webAddItem(res.giDto.catgUserId, res.giDto.itemUserId,
	// res.giDto.itemUserTitle, res.giDto.itemUserUrl);
}

// 删除网址
function ajaxDeleteItem(itemId) {
	if (isEmpty(itemId)) {
		blockAlert('请选择网址');
		return;
	}
	var dt = {
		itemId : itemId
	};
	$.ajax({
		url : rootPath + "/guide/ajaxDeleteItem",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxDeleteItem_back
	});
}

function ajaxDeleteItem_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#guide-item-' + res.itemUserId).remove();
}

// 显示修改网址界面
function showModifyItem(item_id) {
	$('#block-title-item').html("修改网址");
	$('#block-input-add-item').hide();
	$('#block-input-modify-item').show();
	$('#block-input-hidden-item-id').val(item_id);
	$('#guide-catg-select').val('-1');
	var dt = {
		item_id : item_id
	};
	$.ajax({
		url : rootPath + "/guide/ajaxGetItemById",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : showModifyItem_back
	});
	$.blockUI({
		message : $('#add-item'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 500) / 2 + 'px',
			cursor : 'auto',
			width : '500px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
}

function showModifyItem_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	$('#guide-title-item').val(res.gid.itemUserTitle);
	$('#guide-url-item').val(res.gid.itemUserUrl);
	$('#guide-catg-select').val(res.gid.catgUserId);
}

// 修改网址
function ajaxModifyItem() {
	var item_id = $('#block-input-hidden-item-id').val();
	var title = $('#guide-title-item').val();
	var url = $('#guide-url-item').val();
	var catg = $('#guide-catg-select').val();
	if (isEmpty(title)) {
		blockAlert('请填写网址名称');
		$('#guide-title-item').focus();
		return;
	}
	if (isEmpty(url)) {
		blockAlert('请填写网址地址');
		$('#guide-url-item').focus();
		return;
	}
	if (isEmpty(catg) || catg == '-1') {
		blockAlert('请选择网址分类');
		return;
	}
	var dt = {
		item_id : item_id,
		title : title,
		url : url,
		catg : catg
	};
	$.ajax({
		url : rootPath + "/guide/ajaxModifyItem",
		data : dt,
		dataType : "JSON",
		type : "POST",
		success : ajaxModifyItem_back
	});
}

function ajaxModifyItem_back(res) {
	if (res.result != JS_AJAX_SUCCESS) {
		blockAlert(res.msg);
		return;
	}
	closeBlockUI();
	$('#guide-item-a-' + res.giDto.itemUserId + ' > a').attr("href", res.giDto.itemUserUrl);
	$('#guide-item-a-' + res.giDto.itemUserId + ' > a').html(res.giDto.itemUserTitle);
}

// 添加访问网址数
function ajaxAddVisitCnt(item_id) {
	if (isEmpty(item_id)) {
		blockAlert('请选择网址');
		return;
	}
	var dt = {
		item_id : item_id
	};
	$.ajax({
		url : rootPath + "/guide/ajaxAddVisitCnt",
		data : dt,
		dataType : "JSON",
		type : "POST"
	});
}

// 显示添加分类（添加和修改网址界面）
function showAddCatg() {
	$('#addItem-catg1').hide();
	$('#addItem-catg2').show();
	$('#addItem-catg-button1').hide();
	$('#addItem-catg-button2').show();
}

// 隐藏添加分类
function hideAddCatg() {
	$('#addItem-catg2').hide();
	$('#addItem-catg1').show();
	$('#addItem-catg-button2').hide();
	$('#addItem-catg-button1').show();
}

// 显示编辑导航界面
function showModifyGuide() {
	$('#my-guide-show').removeClass("guide-center-normal");
	$('#my-guide-show').addClass("guide-center-nonormal");
	$('.guide-item').css({
		border : "1px solid #eeeeee"
	}).mouseover(function() {
		$(this).css({
			border : "1px solid #aaaaee"
		});
	}).mouseout(function() {
		$(this).css({
			border : "1px solid #eeeeee"
		});
	});
	$('.guide-modify').show();
	$('#guide-title1').hide();
	$('#guide-title2').show();
	$('.guide-item-add').show();
}

// 隐藏编辑导航界面
function hideModifyGuide() {
	$('#my-guide-show').removeClass("guide-center-nonormal");
	$('#my-guide-show').addClass("guide-center-normal");
	$('.guide-item').css({
		border : "1px solid #ffffff"
	});
	$('.guide-item').unbind("mouseover");
	$('.guide-item').unbind("mouseout");
	$('.guide-modify').hide();
	$('#guide-title2').hide();
	$('#guide-title1').show();
	$('.guide-item-add').hide();
}

function blockAlert(msg) {
	$('#block-msg').html(msg);
	$.blockUI({
		message : $('#block-alert'),
		css : {
			top : 100 + 'px',
			left : ($(window).width() - 300) / 2 + 'px',
			cursor : 'auto',
			width : '300px'
		},
		overlayCSS : {
			backgroundColor : '#666666'
		}
	});
	setTimeout($.unblockUI, 1000);
}

// 关闭BlockUI
function closeBlockUI() {
	$.unblockUI();
	$('.block-ui input[type="text"]').val("");
	$('.block-ui input[type="password"]').val("");
	hideAddCatg();
}

// 对导航标题进行控制
function clickHotGuide() {
	$('#hotGuideTitle').removeClass("guide-title-no").addClass("guide-title-focus");
	$('#myGuideTitle').removeClass("guide-title-focus").addClass("guide-title-no");
	hideModifyGuide();
	$('#guide-title1').hide();
	$('#guide-title2').hide();
	$('#hot-guide-show').show();
	$('#my-guide-show').hide();
}
function clickMyGuide() {
	$('#hotGuideTitle').removeClass("guide-title-focus").addClass("guide-title-no");
	$('#myGuideTitle').removeClass("guide-title-no").addClass("guide-title-focus");
	if (isLogin == 1) {
		$('#guide-title1').show();
	}
	$('#hot-guide-show').hide();
	$('#my-guide-show').show();
}